#include "Game.h"
#include <qicon.h>
#include <qpainter.h>
#include <QDebug>
#include <QTimer>
#include "PlayerPlane.h"
#include <QKeyEvent>
#include "Map.h"
#include <QDebug>
#include "Enemy.h"
#include <qrandom.h>
#include "Global.h"
#include "Bullet.h"
#include "score.h"

PlayerPlane *player;
Game::Game(QWidget *parent)
    : QWidget{parent}
{
    QTimer*timer=new QTimer(this);
    timer->callOnTimeout(this,[=]()
                         {
                             //更新数据
                             updateGame();
                             //绘图
                             update();
                         });
    timer->start(1000/60);//游戏帧率 60
}

void Game::run()
{

    this->show();

}

void Game::initGame(int w,int h,const QString &title,const QIcon& icon)
{
    setFixedSize(w,h);
    setWindowTitle(title);
    if(!icon.isNull())
    {
        setWindowIcon(icon);
    }
    WIDTH = w;
    HEIGHT = h;
    globalElapsedTimer.start();
    map=new Map;
    player = Mgr->addEntity(new PlayerPlane(190,600,":/image/me1.png"));
    player->setType(EntityType::ET_Player);


    auto anima=player->addAnimation("normal",new Animation);
    anima->addFrame(":/image/me1.png");
    anima->addFrame(":/image/me2.png");
    player->runAnimation("normal");

    auto animaBoom=player->addAnimation("boom",new Animation(200));
    animaBoom->addFrame(":/image/me_destroy_1.png");
    animaBoom->addFrame(":/image/me_destroy_2.png");
    animaBoom->addFrame(":/image/me_destroy_3.png");
    animaBoom->addFrame(":/image/me_destroy_4.png");

    score = new Score; // 创建Score对象
    score->setPos(10, 40); // 设置Score对象的位置
    // 设置字体






}

void Game::cleanGame()
{
    delete Mgr;
    delete map;
}

void Game::updateGame()
{
    Mgr->refresh();
    map->update();
    Mgr->update();

    //限制飞机的范围
    if(player->position.x()+player->width()/2<0)
    {
        player->position.setX(0-player->width()/2);
    }
    if(player->position.x()+player->width()/2>Game::WIDTH)
    {
        player->position.setX(Game::WIDTH-player->width()/2);
    }
    if(player->position.y()<0)
    {
        player->position.setY(0);
    }
    if(player->position.y()+player->height()>Game::HEIGHT)
    {
        player->position.setY(Game::HEIGHT-player->height());
    }

    static int i=0;

    //发射子弹
    if(i%30==0)
        player->fire();


    //敌机出场
    if(i%60==0)
    {
        QStringList es={":/image/enemy1.png",":/image/enemy2.png",":/image/enemy3_hit.png"};
        auto e =Mgr->addEntity(new Sprite(0,0,es[QRandomGenerator::global()->bounded(3)]));
        e->setPos(QRandomGenerator::global()->bounded(Game::WIDTH-e->width()),-e->height());
        e->velocity.setY(1);
        e->setType(EntityType::ET_Enemy);
    }

    //碰撞检测
    for(auto e:Mgr->getEntityByType<Enemy>(EntityType::ET_Enemy))
    {
        bool isboom=false;
        //子弹与敌机的碰撞
        for(auto b:Mgr->getEntityByType<Bullet>(EntityType::ET_Bullet))
        {
            if(e->collider.intersects(b->collider))
            {
                b->destory();
                e->destory();
                score->increase();
                isboom=true;
                break;
            }
        }
        //玩家与敌机的碰撞
        if(!isboom && player->collider.intersects(e->collider))
        {
            e->destory();
            player->runAnimation("boom",true);


            //player->destory();
            //close();
        }
    }


    i++;
}

void Game::drawGame(QPainter *painter)
{
    map->draw(painter);
    //由manager绘制的，有先后顺序
    Mgr->draw(painter);
    score->draw(painter); // 绘制Score对象


}

void Game::paintEvent(QPaintEvent *ev)
{
    QPainter painter(this);

    drawGame(&painter);
}

void Game::closeEvent(QCloseEvent *ev)
{
    cleanGame();
}

void Game::keyPressEvent(QKeyEvent*ev)
{
    switch (ev->key())
    {
    case Qt::Key_Up:
        player->velocity.setY(-1);
        break;
    case Qt::Key_Down:
        player->velocity.setY(1);
        break;
    case Qt::Key_Left:
        player->velocity.setX(-1);
        break;
    case Qt::Key_Right:
        player->velocity.setX(1);
        break;
    default:
        break;
    }
}

void Game::keyReleaseEvent(QKeyEvent *ev)
{
    switch (ev->key())
    {
    case Qt::Key_Up:
        player->velocity.setY(0);
        break;
    case Qt::Key_Down:
        player->velocity.setY(0);
        break;
    case Qt::Key_Left:
        player->velocity.setX(0);
        break;
    case Qt::Key_Right:
        player->velocity.setX(0);
        break;
    default:
        break;
    }
}
